package secondriver.springsubway.demo;

import org.springframework.beans.BeansException;
import org.springframework.beans.factory.BeanFactory;
import org.springframework.beans.factory.BeanFactoryAware;
import org.springframework.beans.factory.BeanNameAware;
import org.springframework.beans.factory.DisposableBean;
import org.springframework.beans.factory.InitializingBean;
import org.springframework.beans.factory.config.BeanFactoryPostProcessor;
import org.springframework.beans.factory.config.ConfigurableListableBeanFactory;

/***
 * 
 * Spring BeanFactory生命周期
 * 
 * BeanFactory的Bean是延迟初始化Bean,当getBean时进行Bean加载
 * 
 */
public class BeanFactoryLifecycle implements BeanNameAware, BeanFactoryAware,
		BeanFactoryPostProcessor, InitializingBean, DisposableBean {

	private String name;

	public BeanFactoryLifecycle() {
		System.out.println("BeanFactoryLifecycle[constructor]");
	}

	public void setName(String name) {
		this.name = name;
		System.out
				.println("BeanFactoryLifecycle[setName] populate properties.");
	}

	public String getName() {
		System.out.println("BeanFactoryLifecycle[getName] get properties.");
		return name;
	}

	@Override
	public void setBeanName(String name) {
		System.out.println("BeanFactoryLifecycle[setBeanName] " + name);
	}

	@Override
	public void setBeanFactory(BeanFactory beanFactory) throws BeansException {
		System.out.println("BeanFactoryLifecycle[setBeanFactory] "
				+ beanFactory);
	}

	@Override
	public void afterPropertiesSet() throws Exception {
		System.out.println("BeanFactoryLifecycle[afterPropertiesSet]");
	}

	//Bean init-method attribute
	public void init() {
		System.out
				.println("BeanFactoryLifecycle[init] coustom init method called.");
	}

	@Override
	public void destroy() throws Exception {
		System.out.println("BeanFactoryLifecycle[destroy]");
	}

	//Bean destroy-method attribute
	public void close() {
		System.out
				.println("BeanFactoryLifecycle[close] customer close method called.");
	}

	@Override
	public void postProcessBeanFactory(
			ConfigurableListableBeanFactory beanFactory) throws BeansException {
		System.out
				.println("BeanFactoryLifecycle[postProcessBeanFactory] ConfigurableListableBeanFactory = "
						+ beanFactory);

	}
}
